package com.codebase.alicloud.oss;

import com.alibaba.fastjson2.JSONObject;
import com.aliyun.oss.OSSClient;
import com.aliyun.oss.common.utils.BinaryUtil;
import com.aliyun.oss.model.MatchMode;
import com.aliyun.oss.model.PolicyConditions;
import org.apache.http.HttpEntity;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.mime.HttpMultipartMode;
import org.apache.http.entity.mime.MultipartEntityBuilder;
import org.apache.http.entity.mime.content.FileBody;

import java.io.File;
import java.io.UnsupportedEncodingException;
import java.nio.charset.StandardCharsets;
import java.util.Date;
import java.util.HashMap;
import java.util.UUID;

public class PolicyTest {
    public static void main(String[] args) throws UnsupportedEncodingException {

        File file = new File("C:\\Users\\18792\\Desktop\\测试中文文件.pptx");

        JSONObject jsonObject = getPolicy();
        String accessKeyId = jsonObject.getString("accessid");
        String policy = jsonObject.getString("policy");
        String signature = jsonObject.getString("signature");
        String dir = jsonObject.getString("dir");
        String host = jsonObject.getString("host");

        String boundary = UUID.randomUUID().toString();

        dir = dir + file.getName();
        MultipartEntityBuilder builder = MultipartEntityBuilder.create();
        builder.setCharset(StandardCharsets.UTF_8);
        builder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
        builder.setContentType(ContentType.MULTIPART_FORM_DATA);
        builder.setBoundary(boundary);

        builder.addTextBody("OSSAccessKeyId", accessKeyId);
        builder.addTextBody("signature", signature);
        builder.addTextBody("policy", policy);
        builder.addTextBody("key", new String(dir.getBytes(), StandardCharsets.UTF_8), ContentType.create("text/plain", StandardCharsets.UTF_8.toString()));
        builder.addTextBody("key", dir);

        builder.addTextBody("success_action_status", "200");
        //builder.addTextBody("Filename", new String(file.getName().getBytes()), ContentType.create("text/plain", StandardCharsets.UTF_8.toString()));
        builder.addPart("file", new FileBody(file, ContentType.DEFAULT_BINARY));
        HttpEntity entity = builder.build();

        HashMap header = new HashMap<>();
        header.put("Content-Type", "multipart/form-data; boundary=" + boundary);

        JSONObject res = HttpCfgHelper.sendHttp(host, header, entity);
        System.out.println(res.toJSONString());

    }

    public static JSONObject getPolicy() {
        String endpoint = "https://oss-cn-beijing.aliyuncs.com";
        String accessKeyId = "LTAI4G3GZ48UXwVRq4rEinos";
        String accessKeySecret = "NbVsSeucZ9tt162LGECWNfR9ZlcwNc";
        String bucketName = "sc-bill-dev";

        OSSClient ossClient = new OSSClient(endpoint, accessKeyId, accessKeySecret);
        Date now = new Date();
        // 每一天产生一个文件夹
        String dateStr = DateUtil.getDateStr(now, DateStyle.YYYY_MM_DD);
        String dir = dateStr + "/";

        JSONObject jsonObject = new JSONObject();
        //过期时间
        Date expiration = DateUtil.addHour(now, 24);
        //PostObject请求最大可支持的文件大小为5 GB，即CONTENT_LENGTH_RANGE为5*1024*1024*1024。
        PolicyConditions policyConds = new PolicyConditions();
        //设置文件大小限制10M
        policyConds.addConditionItem(PolicyConditions.COND_CONTENT_LENGTH_RANGE, 0, 10485760);
        policyConds.addConditionItem(MatchMode.StartWith, PolicyConditions.COND_KEY, dir);
        String postPolicy = ossClient.generatePostPolicy(expiration, policyConds);
        byte[] binaryData = new byte[0];
        binaryData = postPolicy.getBytes(StandardCharsets.UTF_8);
        String encodedPolicy = BinaryUtil.toBase64String(binaryData);
        String postSignature = ossClient.calculatePostSignature(postPolicy);

        String host = "https://" + bucketName + "." + ossClient.getEndpoint().getHost();
        jsonObject.put("accessid", accessKeyId);
        jsonObject.put("policy", encodedPolicy);
        jsonObject.put("signature", postSignature);
        jsonObject.put("dir", dir);
        jsonObject.put("host", host);
        return jsonObject;
    }
}
